export const formatPrice = (price: number): string => {
  return `¥${(price / 100).toFixed(2)}`
}

export const formatDateTime = (dateTime: string): string => {
  const date = new Date(dateTime)
  const year = date.getFullYear()
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')
  const hour = date.getHours().toString().padStart(2, '0')
  const minute = date.getMinutes().toString().padStart(2, '0')
  
  return `${year}-${month}-${day} ${hour}:${minute}`
}

export const formatDate = (dateStr: string): string => {
  const date = new Date(dateStr)
  const year = date.getFullYear()
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')
  
  return `${year}-${month}-${day}`
}

export const formatTime = (timeStr: string): string => {
  return timeStr.substring(0, 5)
}

export const formatDuration = (minutes: number): string => {
  const hours = Math.floor(minutes / 60)
  const mins = minutes % 60
  
  if (hours > 0) {
    return mins > 0 ? `${hours}小时${mins}分钟` : `${hours}小时`
  }
  return `${mins}分钟`
}

export const getOrderStatusText = (status: string): string => {
  const statusMap = {
    'pending': '待支付',
    'paid': '已支付',
    'confirmed': '已确认',
    'completed': '已完成',
    'cancelled': '已取消',
    'refunded': '已退款'
  }
  
  return statusMap[status as keyof typeof statusMap] || '未知状态'
}

export const getOrderStatusColor = (status: string): string => {
  const colorMap = {
    'pending': '#FF6600',
    'paid': '#1890FF',
    'confirmed': '#52C41A',
    'completed': '#52C41A',
    'cancelled': '#BFBFBF',
    'refunded': '#BFBFBF'
  }
  
  return colorMap[status as keyof typeof colorMap] || '#BFBFBF'
}

export const calculateTotalPrice = (
  originalPrice: number, 
  discountPrice?: number
): number => {
  return discountPrice ? originalPrice - discountPrice : originalPrice
}

export const isOrderCancellable = (order: any): boolean => {
  const cancellableStatuses = ['pending', 'paid']
  const orderDateTime = new Date(`${order.date} ${order.startTime}`)
  const now = new Date()
  const timeDiff = orderDateTime.getTime() - now.getTime()
  const hoursDiff = timeDiff / (1000 * 60 * 60)
  
  return cancellableStatuses.includes(order.status) && hoursDiff > 2
}

export const isOrderRefundable = (order: any): boolean => {
  const refundableStatuses = ['paid', 'confirmed']
  const orderDateTime = new Date(`${order.date} ${order.startTime}`)
  const now = new Date()
  
  return refundableStatuses.includes(order.status) && orderDateTime > now
}